using System;
using Slaks.DocumentSerialization.Document;
using Slaks.DocumentSerialization.TermIdInfo;

namespace Slaks.Graphs.GraphBuilder.Schenker
{
	/// <summary>
	/// Summary description for NSimpleDistanceRepresentationSchenker.
	/// </summary>
	public class NSimpleDistanceRepresentationSchenker : SchenkerBuilder
	{
		#region IRepresentation Members

		protected override Graph Build(Document document, int numberOfVerticesToLeave,int nParameter, TermIdInfoMap map)
		{
			Graph graph = base.Build(document,numberOfVerticesToLeave,nParameter,map);
			string documentName = document.FileName;
			int index = documentName.LastIndexOfAny(new char[] {'\\', '/'});
			if (index >= 0 && (index + 1 < documentName.Length)) documentName = documentName.Substring(index + 1);

			graph.Name = documentName;
			Graph tempGraph = this.BuildTemporalGraph(document, BuilderType.NSimpleDistanceRepresentationSchenker);
			this.RemoveInfrequentVerticesInTemporalGraph(tempGraph, numberOfVerticesToLeave);

			Vertex vertex;
			string sectionType;

			int nDistanceCount;
			//iterate through nodes
			foreach (Node node in document.Nodes)
			{
				//check if the vertex is in the list of frequent nodes.
				if (tempGraph.GetVertex(node.Id) == null) continue;
				vertex = graph.CreateAddVertex(node.Id);
				vertex.Frequency = node.TotalTermFrequency;

				//iterate through sections
				foreach (Section section in node.Sections)
				{
					sectionType = section.GetSectionTypeString();

					Section.SectionType sectionTp = Section.GetSectionTypeByString(sectionType);


					nDistanceCount = 1;
					//iterate through sentences
					foreach (Sentence sentence in section.Sentences)
					{
						AddRelations(document, graph, tempGraph, vertex, nParameter, nDistanceCount, sentence, sectionTp, BuilderType.NSimpleDistanceRepresentationSchenker);
					}
				}
			}
			return graph;
		}
        protected override string GetBuilderName()
        {
            return "Schenker's N-Simple Distance Representation";
        }
        protected override string GetBuilderDescription()
        {
            return "Add edges between vertexes up to a the n-th fartherest vertex, but doesn't label them.";
        }
		#endregion
	}
}